﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WESNet.DNN.Modules.UserExport
{
    public class DateRange : IComparable<DateTime>
    {
        private DateTime? _StartDate = null;
        private DateTime? _EndDate = null;

        public static DateRange Empty = new DateRange();

        public DateRange() { }

        public DateRange(DateTime? startDate, DateTime? endDate)
        {
            _StartDate = startDate;
            _EndDate = endDate;
        }

        public DateRange(DateTime startDate, DateTime endDate)
        {
            StartDate = startDate;
            EndDate = endDate;
        }

        public DateTime StartDate
        {
            get
            {
                return StartDateIsNull ? DateTime.MinValue : (DateTime)_StartDate;
            }
            set
            {
                _StartDate = value;
            }
        }

        public DateTime EndDate
        {
            get
            {
                return EndDateIsNull ? DateTime.MaxValue : (DateTime)_EndDate;
            }
            set
            {
                _EndDate = value;
            }
        }

        public bool StartDateIsNull
        {
            get
            {
                return !_StartDate.HasValue;
            }
        }

        public bool EndDateIsNull
        {
            get
            {
                return !_EndDate.HasValue;
            }
        }

        public bool HasValue
        {
            get
            {
                return _StartDate.HasValue && _EndDate.HasValue;
            }
        }

        public int CompareTo(DateTime other)
        {
            var result = 0;

            if (other < StartDate)
            {
                result = -1;
            }
            else if (other > EndDate )
            {
                result = 1;
            }

            return result;
        }

        public bool InRange(DateTime other)
        {
            return CompareTo(other) == 0;
        }
    }
}